﻿using GetData.webProductClass;
using jutehui.GetData;
using jutehui.Model;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;

namespace GetData
{
    public class jinrijuchaozhi
    {
        HtmlAgilityPack.HtmlDocument doc;
        public jinrijuchaozhi()
        {
            doc = new HtmlAgilityPack.HtmlDocument();
        }
        /// <summary>
        /// 通过网页html获取商品信息并保存到数据库中
        /// </summary>
        /// <param name="url"></param>
        /// <param name="shoppingType">海淘2 精选1</param>
        /// <returns></returns>
        public string GetProductsToDataBase(string url)
        {

            var htmlText = CustomMethod.GetHtmlByHttpHelper(url);
            string msg = "";
            var pList = GetProductByRegex(htmlText);
            if (pList == null)
            {
                return "";
            }
            DataTable dt = CustomMethod.ListToDataTable<Product>(pList, "Product", ref msg);
            if (dt == null || dt.Rows.Count < 1)
            {
                return "";
            }
            jutehui.BLL.Product bll = new jutehui.BLL.Product();
            //string msg = "";
            if (bll.SaveTableData("Product", dt, out msg))
            {
                return dt.Rows.Count.ToString(); ;
            }
            else
            {
                msg = "今日聚超值：GetProductsToDataBase" + msg;
                CustomMethod.WriteLog(msg);
                return "";

            }

        }
        /// <summary>
        /// 根据正则解析商品信息
        /// </summary>
        /// <param name="htmlText"></param>
        /// <param name="type">海购还是精选</param>
        /// <returns></returns>
        public List<Product> GetProductByRegex(string htmlText)
        {
            List<Product> pList = new List<Product>();

            // string productRegex = @"<div class=""art"" id=""(.|\s)*?"" tid=""(.|\s)*?"" name=""(.|\s)*?"">[\s\S]*?<!-- 文章列表开始 -->";
            doc.LoadHtml(htmlText);
            var listHtml = doc.DocumentNode.SelectNodes("//div[@class='art']");//CustomMethod.GetInfoByRegex(productRegex, htmlText, 0);
            if (listHtml == null || listHtml.Count < 1)
            {
                CustomMethod.WriteLog("今日聚超值：GetProductByRegex 网站没有解析出商品信息，请检查规则。");
                return null;
            }
            var list = new List<string>();
            for (int index = 0; index < listHtml.Count; index++)
            {
                list.Add(listHtml[index].InnerHtml);
            }
            ProductTypeEnum pEnum = new ProductTypeEnum();
            for (int index = 0; index < list.Count; index++)
            {
                Product product = new Product();
                try
                {
                    product = GetProduct(list[index]);
                    if (product == null)
                    {
                        continue;
                    }
                    //-----------------------------初始化初始值-------------------------------
                    //product.FromWeb = "今日聚超值";
                    //product.UpdateTime = DateTime.Now;
                    //product.UpdateTimeStr = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
                    ////------------------------------------------------------------------------------
                    //string tagRegex = @"<span class=""tag-cati"">([^<]+)</span>"; //查看是否为国内优惠信息
                    //var tag = CustomMethod.GetInfoByRegex(tagRegex, list[index], 0);
                    //if (tag == null || tag.Count == 0 || tag[0].Equals("") || !tag[0].Trim().Contains("优惠")) //tag[0].Trim()!="海淘"
                    //{
                    //    continue;
                    //}


                    ////是否为商品优惠信息，如果是则获取商品源url
                    //string urlRegex = @"<div class=""art-link"">[\s\S]*?</div>";
                    //var urlContentStr = CustomMethod.GetInfoByRegex(urlRegex, list[index], 0);
                    //if (urlContentStr == null || urlContentStr.Count < 1)
                    //{
                    //    CustomMethod.WriteLog("今日聚超值:未获取商品来源url区域");
                    //    continue;
                    //}
                    //var herfList = CustomMethod.GetInfoFroma(urlContentStr[0]);
                    //if (herfList == null || herfList.Count < 1)
                    //{
                    //    CustomMethod.WriteLog("今日聚超值:未获取商品来源url相关链接");
                    //    continue;
                    //}
                    ////商品链接
                    //string productUrl = "";
                    //product.Plink = CustomMethod.GetUrlByCurrentUrl(herfList[0], ref productUrl);
                    //if (product.Plink == "")
                    //{
                    //    if (productUrl == "")
                    //    {
                    //        CustomMethod.WriteLog("今日聚超值：GetProductByRegex 网站没有解析出商品链接，请检查规则。");
                    //    }
                    //    continue;
                    //}
                    //product.Memo = productUrl;
                    ////定位图片链接区域
                    //string imgContentRegex = @"<div class=""art-pic"">[\s\S]*?</div>";
                    //var imgContentStr = CustomMethod.GetInfoByRegex(imgContentRegex, list[index], 0);
                    //if (imgContentStr == null || imgContentStr.Count < 1)
                    //{
                    //    CustomMethod.WriteLog("今日聚超值：GetProductByRegex 网站没有解析出商品图片区域，请检查规则。");
                    //    continue;
                    //}
                    // string imgUrl = CustomMethod.GetImgUrl(list[index], "今日聚超值");
                    ////if (imgUrl == "")
                    ////{
                    ////    continue;
                    ////}
                    //string detailText = "";
                    //if (imgUrl.Contains("http"))
                    //{
                    //    product.ImgUrl = CustomMethod.SavePic("jrjcz", imgUrl);
                    //}
                    //else
                    //{
                    //    product.ImgUrl = CustomMethod.SavePic("jrjcz", "http:" + imgUrl);
                    //}

                    ////获取商品来源
                    //string sourceRegex = @"<div class=""art-hd"">[\s\S]*?</div>";
                    //var sourceList = CustomMethod.GetInfoByRegex(sourceRegex, list[index], 0);
                    //if (sourceList != null && sourceList.Count > 0)
                    //{
                    //    var sourHer = CustomMethod.GetInfoFroma(sourceList[0]);
                    //    if (sourHer != null && sourHer.Count > 1)
                    //    {
                    //        //来源
                    //        product.Source = sourHer[1].Trim();
                    //        //标题

                    //        string name = System.Web.HttpUtility.HtmlDecode(sourHer[sourHer.Count - 1]);
                    //        int star = name.IndexOf("\">");
                    //        int end = name.IndexOf("</");
                    //        string price = name.Substring(star + 2, end - star - 2).Replace("\n", "").Replace("\r", "").Replace("\t", "").Trim();

                    //        product.Pname = name;
                    //        product.Price = price;


                    //        ////内容
                    //        //string detail = sourHer[sourHer.Count - 2].Trim();

                    //        //detailText = CustomMethod.GetHtmlByHttpHelper(detail);

                    //        //string regcon = @"<div class=""art-con-des"">[\s\S]*?</div>";
                    //        //var cont = CustomMethod.GetInfoByRegex(regcon, detailText, 0);
                    //        //if (cont == null || cont.Count == 0 || cont[0].Equals(""))
                    //        //{
                    //        //    continue;
                    //        //}
                    //        ////过滤 a  标签跳转
                    //        //product.Description = cont[0].Replace("【PConline 聚超值】", "");
                    //    }

                    //}


                    //////标题 商品精选 海淘分类
                    ////string titRegex = @" <div class=""art-hd"">[\s\S]*?</div>";
                    ////var titContent = CustomMethod.GetInfoByRegex(titRegex, list[index], 0);
                    ////if (titContent == null || titContent.Count < 1)
                    ////{
                    ////    CustomMethod.WriteLog("今日聚超值：GetProductByRegex 网站没有解析出商品标题区域，请检查规则。");
                    ////    continue;
                    ////}
                    ////var herfContent = CustomMethod.GetInfoFroma(titContent[0]);
                    ////if (herfContent == null || herfContent.Count < 4)
                    ////{
                    ////    CustomMethod.WriteLog("今日聚超值：GetProductByRegex 网站没有解析出商品标题信息，检查规则。");
                    ////    continue;
                    ////}
                    ////// 权力的游戏 DVD 光碟                                                 
                    //////  <i class="red">&nbsp;&nbsp;&nbsp;32.99+8.98美元直邮约￥267</i>
                    ////int startIndex = herfContent[3].IndexOf("<i class=\"red\">");
                    ////if (startIndex > -1)
                    ////{
                    ////    string title = herfContent[3].Substring(0, startIndex);
                    ////    product.Pname = Regex.Replace(title, @"\s+", "");
                    ////    string price = herfContent[3].Substring(title.Length, herfContent[3].Length - title.Length);
                    ////    product.Price = price.Replace("<i class=\"red\">", "").Replace("&nbsp;", "").Replace("</i>", "");
                    ////}
                    ////else
                    ////{
                    ////    continue;
                    ////}
                    ////string naviType = Regex.Replace(herfContent[1], @"\s+", "");
                    ////if (naviType == "【海淘】")
                    ////{
                    ////    product.TypeId = (int)MallsEnumClass.NavigationType.海淘;
                    ////}
                    ////else
                    ////{
                    ////    product.TypeId = (int)MallsEnumClass.NavigationType.精选;
                    ////}
                    //////获取描述信息
                    ////string desRegex = @"<div class=""art-pre"">[\s\S]*?</div>";
                    ////var desList = CustomMethod.GetInfoByRegex(desRegex, list[index], 0);
                    ////if (desList == null || desList.Count < 1)
                    ////{
                    ////    product.Description = product.Pname;
                    ////}
                    ////else
                    ////{
                    ////    string content = desList[0].Replace("<br/>", "|");
                    ////    string filterBlank = Regex.Replace(content, @"\s+", "");
                    ////    var contentList = filterBlank.Split('|').ToList();
                    ////    string pureContent = "";
                    ////    for (int num = 0; num < contentList.Count; num++)
                    ////    {
                    ////        pureContent += "|||" + Regex.Replace(contentList[num], @"<(.[^>]*)>", "").Replace("&nbsp;", "").Replace("&#x27;", "'"); 
                    ////    }
                    ////    // string filterHtmlLabel = Regex.Replace(desList[0], @"<(.[^>]*)>", "").Replace("&nbsp;", "");
                    ////    // string filterBlank = Regex.Replace(content, @"\s+", "");
                    ////    //int endIndex = filterBlank.LastIndexOf("目前");

                    ////    //if (endIndex > 0)
                    ////    //{
                    ////    //    pureContent = filterBlank.Substring(0, endIndex);
                    ////    //}
                    ////    //else
                    ////    //{

                    ////    //}

                    ////    product.Description = pureContent == "" ? product.Pname : pureContent;
                    ////}
                    ////-------------------------获取商品分类--------------------------
                    //var herfTypeList = CustomMethod.GetInfoFroma(list[index]);
                    //if (herfTypeList.Count > 0)
                    //{

                    //    string regex = @"<div class=""tips-left"">[\s\S]*?</div>";
                    //    product.ProductTypeId = pEnum.GetProductTypeId(detailText, regex, typeof(ProductTypeEnum.jrjczType), 0);


                    //}
                    ////-----------------------------------------------------------------
                    ////   }
                    //product.Pname = System.Web.HttpUtility.HtmlEncode(product.Pname);
                    ////去掉标签
                    //int start = product.Pname.IndexOf("&lt");
                    //if(start>0)
                    //{
                    //   string newName = product.Pname.Substring(0,start-3);
                    //   product.Pname = newName;
                    //}



                    //  product.Description.Replace("&#x2F;", "/").Replace("#039;", "'");
                }
                catch (Exception ex)
                {
                    //错误日志 增加具体哪个商品信息
                    CustomMethod.WriteLog("今日聚超值：GetProductByRegex" + ex.Message.ToString());
                    continue;
                }
                pList.Add(product);

            }
            var newList = CustomMethod.GetDistinctProdcuts(pList);
            newList.Reverse();
            return newList;
        }

        /// <summary>
        ///根据规则获取属性值 一个商品item
        /// </summary>
        /// <param name="htmlContent"></param>
        /// <returns></returns>
        public Product GetProduct(string htmlContent)
        {
            Product pro = new Product();
            pro.FromWeb = "今日聚超值";
            pro.UpdateTime = DateTime.Now;
            pro.UpdateTimeStr = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");

            pro.TypeId = 1;
            doc.LoadHtml(htmlContent);
            //名称
            var namelist = doc.DocumentNode.SelectNodes("//div[@class='art-hd']/a");
            if (namelist != null && namelist.Count > 0)
            {
                pro.Pname = namelist[0].InnerText.Trim().Replace("&nbsp;", "");
            }

            //图片
            var picL = CustomMethod.GetAttr(doc, htmlContent, "//div[@class='art-pic']/a/img", "src");
            if (picL != null && picL.Count > 0)
            {
                pro.ImgUrl = "http:" + picL[0];
            }
            //价格
            var priL = doc.DocumentNode.SelectNodes("//i[@class='fc-red']");
            if (priL != null && priL.Count > 0)
            {
                pro.Price = priL[0].InnerText.Trim().Replace("&nbsp;", "");
            }
            //数据源
            var sourL = doc.DocumentNode.SelectNodes("//p[@class='art-info']/a");
            if (sourL != null && sourL.Count > 0)
            {
                pro.Source = sourL[0].InnerText.Trim();
            }
            var detailurl = CustomMethod.GetAttr(doc, htmlContent, "//div[@class='art-hd']/a", "href");
            string conthtml = "";//详细页面
            //详细
            if (detailurl != null && detailurl.Count > 0)
            {
                string durl = "http:" + detailurl[0];
                conthtml = CustomMethod.GetHtmlByHttpHelper(durl);
                doc.LoadHtml(conthtml);
                var desList = doc.DocumentNode.SelectNodes("//div[@class='art-con-des']/p");
                string desStr = "";
                if (desList != null)
                {
                    for (int k = 0; k < desList.Count; k++)
                    {
                        if (desList[k].InnerText.Trim() == "")
                        {
                            continue;
                        }
                        desStr += "<p>" + desList[k].InnerText + "</p>";
                    }
                    pro.Description = desStr;
                }
                else
                {
                    pro.Description = pro.Pname;
                }
                //链接 
                var urL = CustomMethod.GetAttr(doc, conthtml, "//a[@class='btn btn-red']", "href");
                if (urL != null && urL.Count > 0)
                {
                    string dirUrl = "http:" + urL[0];
                    if (urL[0].Contains("http"))
                    {
                        dirUrl = urL[0];
                    }
                    string productUrl = "";
                    string trueUrl = CustomMethod.GetUrlByCurrentUrl(dirUrl, ref productUrl);
                    pro.Memo = productUrl;
                    pro.Plink = trueUrl;
                    if (pro.Plink == "")
                    {
                        if (productUrl == "")
                        {
                            CustomMethod.WriteLog("今日聚超值：没有抓取到商品链接");
                        }
                        return null;
                    }
                }
                else
                {
                    return null;
                }

                ////分类
                var ty = doc.DocumentNode.SelectNodes("//i[@class='tips-link']/a");
                if (ty != null && ty.Count > 3)
                {

                    // var tagList = ty.Trim().Split(' ').ToList();
                    var tag = ty[2].InnerText;
                    var tyid = ProductTypeEnum.GetProductType(tag, typeof(ProductTypeEnum.jrjczType));
                    pro.ProductTypeId = tyid;
                }
                else
                {
                    pro.ProductTypeId = 16;
                }
                //详细图
                var despics = CustomMethod.GetAttr(doc, conthtml, "//div[@class='art-con-des']/p/img", "src");
                if (despics != null)
                {
                    if (despics.Count == 1)
                    {
                        pro.ImgUrl2 = despics[0];
                    }
                    if (despics.Count == 2)
                    {
                        pro.ImgUrl2 = despics[0];
                        pro.ImgUrl3 = despics[1];
                    }
                    else if (despics.Count >= 3)
                    {
                        pro.ImgUrl2 = despics[0];
                        pro.ImgUrl3 = despics[1];
                        pro.ImgUrl4 = despics[2];
                    }
                }
            }
            else
            {
                return null;
            }
            return pro;
        }

    }
}
